#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <future>
#include <functional>
#include <thread>


#if 0
/******** Module Control Interface *************/
void MainWindow::Initialization()
{
    glabalInfo.subModStus.InitializationStus = 0;
    qDebug()<<"Initialization"<<endl;

    setEPC(0, 0);
    delay(200);
    setEPC(1, 0);
    delay(200);
    setEPC(2, 68);
    delay(5000); //wait for stabilization
    ReadEPC(EPC_MODE_PRESS); //
    //read 3 EPC

    setValve(1, 0);
    setValve(2, 0);
    setValve(3, 0);
    setValve(4, 0);
    setValve(5, 0);
    setValve(6, 0);
    setValve(7, 0);
    setValve(8, 0);

  //  setMFC(0);
    MainWindow_log("Initialization Finished...");
    qDebug()<<"Initialization end"<<endl;
    glabalInfo.subModStus.InitializationStus = 1; //indicate Initial finish
}

void MainWindow::PurgeDehydration()
{
    glabalInfo.subModStus.PurgeDehydrationStus = 0;
    setValve(1, 1);
    setHeater(0,3.0);
    setValve(5, 1);
    delay(1000); //delay X s

    setValve(5, 0);
    delay(1000);
    setValve(1, 0);
    MainWindow_log("PurgeDehydration Finished...");
    glabalInfo.subModStus.PurgeDehydrationStus = 1;
}

void MainWindow::PurgeConcentrationA()
{
#if 1
    glabalInfo.subModStus.PurgeConcentrationAStus = 0;
    setValve(6, 0);
    delay(2000);

    setValve(8, 0);
    delay(2000);

    setEPC(1, 68);
    delay(5000);

    setHeater(1,3.0);
    delay(1000);//delay X s
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PurgeConcentrationA Finished...");
    glabalInfo.subModStus.PurgeConcentrationAStus = 1;
#endif
//    glabalInfo.subModStus.PurgeConcentrationAStus = 0;
//    qDebug()<<"A1"<<endl;
//    setValve(6, 0);
//    delay(2000);

//    qDebug()<<"A2"<<endl;
//    setValve(8, 0);
//    delay(2000);
//    glabalInfo.subModStus.PurgeConcentrationAStus = 1;
}

void MainWindow::PurgeConcentrationB() //暂时不用
{
#if 1
    glabalInfo.subModStus.PurgeConcentrationBStus = 0;
    setValve(6, 1); //ERV ON
    delay(2000);
    setValve(8, 0);
    delay(2000);
    setEPC(1, 68);
    delay(5000);
    //setHeater(2,3.0);
    delay(1000); //delay X second
    setEPC(1, 0);
    delay(5000);
    setValve(6, 0); //ERV OFF
    delay(2000);
    MainWindow_log("PurgeConcentrationB Finished...");
    glabalInfo.subModStus.PurgeConcentrationBStus = 1;
#endif
//    glabalInfo.subModStus.PurgeConcentrationBStus = 0;
//    qDebug()<<"B1"<<endl;
//    setValve(6, 1);
//    delay(2000);

//    qDebug()<<"B2"<<endl;
//    setValve(8, 1);
//    delay(2000);
//    glabalInfo.subModStus.PurgeConcentrationBStus = 1;
}

void MainWindow::PurgeFocusing() //暂时不用
{
    glabalInfo.subModStus.PurgeFocusingStus = 0;
    setValve(6, 0); //ERV ON
    delay(2000);
    setValve(8, 0);
    delay(2000);
    setEPC(1, 68);
    delay(5000);
    //setHeater(3,3.0);
    delay(3000); //delay X second
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PurgeFocusing Finished...");
    glabalInfo.subModStus.PurgeFocusingStus = 1;
}

void MainWindow::PreSampleA() //暂时不用
{
    glabalInfo.subModStus.PreSampleAStus = 0;
    setValve(3, 1);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y seconds
    setValve(7, 0);
    delay(1000);
    MainWindow_log("PreSampleA Finished...");
    glabalInfo.subModStus.PreSampleAStus = 1;
}
void MainWindow::PreSampleB()
{
    glabalInfo.subModStus.PreSampleBStus = 0;
    setValve(3, 0);
    setValve(6, 1);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y seconds
    setValve(7, 0);
    delay(1000);
    MainWindow_log("PreSampleB Finished...");
    glabalInfo.subModStus.PreSampleBStus = 1;
}
void MainWindow::SampleA()
{
    glabalInfo.subModStus.SampleAStus = 0;
    setValve(3, 0);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(3000);  //Delay Y minutes
    setValve(7, 0);
    delay(1000);
    MainWindow_log("SampleA Finished...");
    glabalInfo.subModStus.SampleAStus = 1;
}
void MainWindow::SampleB()
{
    glabalInfo.subModStus.SampleBStus = 0;
    setValve(3, 1);
    setValve(6, 1);
    delay(2000);
    setMFC(500);  //Set MFC to X sccm
    setValve(7, 1);
    delay(60000);  //Delay Y minutes
    setValve(7, 0);
    delay(1000);
    MainWindow_log("SampleB Finished...");
    glabalInfo.subModStus.SampleBStus = 1;
}
void MainWindow::PrePurgeA()
{
    glabalInfo.subModStus.PrePurgeAStus = 0;
    setValve(6, 1);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y seconds
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("PrePurgeA Finished...");
    glabalInfo.subModStus.PrePurgeAStus = 1;
}
void MainWindow::PrePurgeB()
{
    glabalInfo.subModStus.PrePurgeBStus = 0;
    setValve(6, 0);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    MainWindow_log("PrePurgeB Finished...");
    glabalInfo.subModStus.PrePurgeBStus = 1;
}
void MainWindow::ConTrapFireA()
{
    glabalInfo.subModStus.ConTrapFireAStus = 0;
    setValve(6, 1);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    setHeater(1, 3.5);  //Set Heater 1 to Timer Y
    delay(4000);
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("ConTrapFireA Finished...");
    glabalInfo.subModStus.ConTrapFireAStus = 1;
}
void MainWindow::ConTrapFireB()
{
    glabalInfo.subModStus.ConTrapFireBStus = 0;
    setValve(6, 0);
    delay(2000);
    setEPC(1, 68); //Set EPC 1 to X psi
    delay(5000);   //delay Y second
    //setHeater(2, 3.5);  //Set Heater 1 to Timer Y
    delay(4000);
    setEPC(1, 0);
    delay(5000);
    MainWindow_log("ConTrapFireB Finished...");
    glabalInfo.subModStus.ConTrapFireBStus = 1;
}
void MainWindow::FocusTrapFire()
{
    glabalInfo.subModStus.FocusTrapFireStus = 0;
    setValve(8, 1);
    setEPC(2, 68); //Set EPC 2 to X psi
    delay(5000);
    //setHeater(3, 3.5);
    delay(5000);
    setValve(8, 0);
    delay(2000);
    MainWindow_log("FocusTrapFire Finished...");
    glabalInfo.subModStus.FocusTrapFireStus = 1;
}
void MainWindow::StandardA()
{
    glabalInfo.subModStus.StandardAStus = 0;

    qDebug()<<"StandardA"<<endl;

    setValve(2, 1);
    setValve(4, 1);
    delay(2000);
    setValve(3, 0);
    setValve(6, 0);
    delay(2000);
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(2000);  //Sample Y minutes
    setValve(7, 0);
    delay(2000);
    setValve(4, 0);
    setValve(2, 0);
    MainWindow_log("StandardA Finished...");
    glabalInfo.subModStus.StandardAStus = 1;
}
#endif

ERR_STATUS MainWindow::StandardB2()
{
//    glabalInfo.subModStus.StandardBStus = 0;
    setValve(2, 1);
    setValve(4, 1);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    setValve(3, 1);
    setValve(6, 1);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    setMFC(100);  //Set MFC to X sccm
    setValve(7, 1);
    delay(2000);  //Sample Y minutes
    setValve(7, 0);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    setValve(4, 0);
    setValve(2, 0);
    MainWindow_log("StandardB Finished...");
//    glabalInfo.subModStus.StandardBStus = 1;

    return err_none;
}


ERR_STATUS MainWindow::readGC_stus2()
{
    glabalInfo.subModStus.GC_readStus = 0;
    quint16 addr_GC_read_status = 14;
    //quint16 GC_read_status = 2; //2     indicate unknown status
    mutex_epcmfc->lock();
    GC_read_status = sendReadHoldingReg(SerDev_EPC_MFC, EPC_MB_SLAVE_ID, addr_GC_read_status);
    mutex_epcmfc->unlock();
    qDebug()<<"GC_read_status: "<<GC_read_status<<endl;
    switch(GC_read_status)
    {
     case 0:
        qDebug()<<"GC ready"<<endl;
        MainWindow_log("readGCInfo: GC ready");
        break;
     case 1:
       qDebug()<<"GC not-ready"<<endl;
       MainWindow_log("readGCInfo: GC not-ready");
        break;
    default:
        qDebug()<<"GC unkonwn"<<endl;
        MainWindow_log("readGCInfo: GC unkonwn");
        break;
    }

    return err_none;
}




/*---------- Synchronize ----------
    LiuMing new add for synchronize execute FlowControl @2021-05-10
In this way, we can easily use std::future && std::promise

*/
void MainWindow::thread_run()
{
    qDebug()<<"std thread start***********"<<endl;

    std::unique_lock<std::mutex> locker(trig_mutex);
    for(;;)
    {
        cv.wait(locker);

        while(threadTrigFlag)
        {

            std::this_thread::sleep_for(std::chrono::seconds(2));
            qDebug()<<"std thread end++++++++++++"<<endl;
        }
    }
}

